/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package frm.billingAndPaymentProcessing.managedbean;

import commonInfrastructure.accountManagement.session.EmployeeSessionBean;
import frm.billingAndPaymentProcessing.entity.IncomingBillEntity;
import frm.billingAndPaymentProcessing.entity.VoucherEntity;
import frm.billingAndPaymentProcessing.session.IncomingBillSessionBean;
import frm.billingAndPaymentProcessing.session.VoucherSessionBean;
import java.io.IOException;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;

/**
 *
 * @author xhx
 */
@ManagedBean
@ViewScoped
public class VoucherConfirmationManagedBean {

    private VoucherEntity voucher;
    private IncomingBillEntity incomingBill;
    private String employeeName;
    @EJB
    VoucherSessionBean voucherSessionBean;
    @EJB
    IncomingBillSessionBean incomingBillSessionBean;
    @EJB
    EmployeeSessionBean employeeSessionBean;

    /** Creates a new instance of VoucherConfirmationManagedBean */
    public VoucherConfirmationManagedBean() {
    }

    public void initView(PhaseEvent evnet) {
        if (voucher == null) {
            setVoucher((VoucherEntity) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("voucher"));
        }
        if (incomingBill == null) {
            incomingBill = (IncomingBillEntity) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("incomingBillRecord");
        }
        setEmployeeName(employeeSessionBean.getEmployeeById(getVoucher().getIssuedBy()).getName());
    }

    public void confirm() throws IOException {
        voucher.setIncomingbill(incomingBill);
        voucherSessionBean.createVoucher(voucher);
        incomingBill.setVoucher(voucher);
        incomingBill.addPayment(getVoucher().getAmountPaid());
        incomingBillSessionBean.saveChanges(incomingBill);
        FacesContext.getCurrentInstance().getExternalContext().getFlash().put("voucher", getVoucher());
        FacesContext.getCurrentInstance().getExternalContext().redirect("displayVoucher.xhtml");
    }

    public void back() throws IOException {
        FacesContext.getCurrentInstance().getExternalContext().getFlash().put("incomingBill", incomingBill);
        FacesContext.getCurrentInstance().getExternalContext().getFlash().put("voucher", getVoucher());
        FacesContext.getCurrentInstance().getExternalContext().redirect("displayIncomingBill.xhtml");
    }

    public IncomingBillEntity getIncomingBill() {
        return incomingBill;
    }

    public void setIncomingBill(IncomingBillEntity incomingBill) {
        this.incomingBill = incomingBill;
    }

    public String getEmployeeName() {
        return employeeName;
    }

    public void setEmployeeName(String employeeName) {
        this.employeeName = employeeName;
    }

    public VoucherEntity getVoucher() {
        return voucher;
    }

    public void setVoucher(VoucherEntity voucher) {
        this.voucher = voucher;
    }
}
